//
//       _/_/_/                      _/            _/_/_/_/_/
//    _/          _/_/      _/_/    _/  _/              _/      _/_/      _/_/
//   _/  _/_/  _/_/_/_/  _/_/_/_/  _/_/              _/      _/    _/  _/    _/
//  _/    _/  _/        _/        _/  _/          _/        _/    _/  _/    _/
//   _/_/_/    _/_/_/    _/_/_/  _/    _/      _/_/_/_/_/    _/_/      _/_/
//
//
//  Copyright (c) 2015-2016, Geek Zoo Studio
//  http://www.geek-zoo.com
//
//
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
//  IN THE SOFTWARE.
//

package com.ledim.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class LocationManager implements BDLocationListener {

    public LocationClient mLocationClient = null;
    private static LocationManager Instance;

    private String city;
    public static Context context;

    private BDLocationListener mLocationListener;

    public LocationManager(Context context) {
        LocationManager.context = context;

        Instance = this;
        mLocationClient = new LocationClient(context);

        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
        option.setCoorType("gcj02");
        option.setIsNeedAddress(true);
        option.setNeedDeviceDirect(false);
        //option.setProdName(Config.BAIDU_MAP_PRODNAME);
        option.setOpenGps(true);
        mLocationClient.setLocOption(option);
        mLocationClient.registerLocationListener(this);
        mLocationClient.start();

    }

    public static LocationManager getInstance() {
        return Instance;
    }

    public void refreshLocation() {
        mLocationClient.requestOfflineLocation();
    }

    public void refreshLocation(BDLocationListener listener) {
        mLocationListener = listener;
        mLocationClient.requestOfflineLocation();
    }

    public BDLocation getLocation() {
        return mLocationClient.getLastKnownLocation();
    }

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        if (bdLocation == null) {
            return;
        }
        StringBuffer sb = new StringBuffer(256);
        sb.append("time : ");
        sb.append(bdLocation.getTime());
        sb.append("\nerror code : ");
        sb.append(bdLocation.getLocType());
        sb.append("\nlatitude : ");
        sb.append(bdLocation.getLatitude());
        sb.append("\nlontitude : ");
        sb.append(bdLocation.getLongitude());
        sb.append("\nradius : ");
        sb.append(bdLocation.getRadius());
        if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
            sb.append("\nspeed : ");
            sb.append(bdLocation.getSpeed());
            sb.append("\nsatellite : ");
            sb.append(bdLocation.getSatelliteNumber());
            sb.append("\ndirection : ");
            sb.append("\naddr : ");
            sb.append(bdLocation.getAddrStr());
        } else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
            sb.append("\naddr : ");
            sb.append(bdLocation.getAddrStr());
        }
        String city = bdLocation.getCity();
        if (!TextUtils.isEmpty(city)) {
            if (city.length() > 1) {
                //city= city.substring(0,city.length()-1);
                LedimSpUtils.put("cityname", city);
            }

        } else {
            if (mLocationClient.isStarted()) {

                //                int result =mLocationClient.requestOfflineLocation();
                //                Log.d("LocSDK3", "result:" + result);

            } else {
                Log.d("LocSDK3", "locClient is null or not started");
            }

        }

        if (null != mLocationListener) {
            mLocationListener.onReceiveLocation(bdLocation);
        }
        //        Log.e("location",bdLocation.getCity());
    }

    public void destory() {
        mLocationClient.stop();
    }

}
